package com.ityang.hhhh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ityang.hhhh.conm.R;
import com.ityang.hhhh.domain.Journalism;
import com.ityang.hhhh.service.IJournalismService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *  新闻
 * @author ityang
 * @since 2022-05-18
 */
@RestController
@RequestMapping("/journalism")
public class JournalismController {

    @Autowired
    private IJournalismService journalismService;

    /**
     * 查询新闻
     * @return
     */
    @GetMapping("/xin")
    public R xin(){
        int page = 1;
        int pageSize = 10;
        List<Journalism> journalismList = journalismService.list();
        return R.success(journalismList);
    }

    /**
     * 添加新闻
     * @param journalism
     * @return
     */
    @PostMapping("/add")
    public R save(@RequestBody Journalism journalism){
        journalismService.save(journalism);
        return R.success("插入成功");

    }

    /**
     * 条件查询新闻
     * @param journalism
     * @return
     */
    @GetMapping("/quit")
    public R get(@RequestBody Journalism journalism){
        try{
            LambdaQueryWrapper<Journalism> lqw = new LambdaQueryWrapper<>();
            lqw.like(journalism.getTitle() !=null,Journalism::getTitle,journalism.getTitle())
            .or().like(journalism.getDescription() !=null ,Journalism::getDescription,journalism.getDescription())
            .or().like(journalism.getSource() !=null ,Journalism::getSource,journalism.getSource());
            List<Journalism> journalismList = journalismService.list(lqw);
            return R.success(journalismList);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("查询失败");
        }

    }

}

